﻿using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;
using WebApiV1.Data;
using WebApiV1.Data.Entity;
using WebApiV1.Domain;

namespace WebApiV1.Utils
{
    public class DbInitializeHelper
    {
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var scope = serviceProvider.CreateScope())
            {
                var db = scope.ServiceProvider.GetService(typeof(DbAdmin1200Context)) as DbAdmin1200Context;

                var _userRespository = scope.ServiceProvider.GetService(typeof(IRespository<User>)) as EfRespository<User>;

                var _roleRespository = scope.ServiceProvider.GetService(typeof(IRespository<Role>)) as EfRespository<Role>;

                db.Database.EnsureCreated();

                var tempUser = db.Users.Any();

                if (!tempUser)
                {
                    var role = new Role
                    {
                        RoleName="管理员",
                        description="区域管理员"
                    };

                    _roleRespository.Insert(role);

                    _userRespository.InsertBulk(new User[]
                    {
                        new User
                        {
                            UserName="小彭",
                            PassWord="123456",
                            RoleId=role.Id
                        },
                        new User
                        {
                            UserName="小梅",
                            PassWord="123456",
                            RoleId=role.Id
                        }
                    });
                }
            }
        }
    }
}
